package com.cuitrip.business.bill;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.utils.DisplayUtil;
import com.lab.utils.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sheena on 16/4/28.
 */
public class WalletHintActivity extends CustomUiFragmentActivity {

    @Bind(R.id.vp_wallet)
    ViewPager mViewPager;

    @Override
    public boolean initCloseTopBar() {
        return true;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.acitivity_wallet_hint);
    }

    @Override
    protected boolean appTheme() {
        return false;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        MyAdapter myAdapter = new MyAdapter();
        mViewPager.setAdapter(myAdapter);
        SharedPreferences sp = getSharedPreferences("wallet_guide", Context.MODE_PRIVATE);
        sp.edit().putBoolean("isShow",false).commit();
    }

    @Override
    public void initListener() {
    }

    public class MyAdapter extends PagerAdapter {

        private static final int PAGE_SIZE =4 ;

        public MyAdapter() {
        }

        @Override
        public int getCount() {
            return PAGE_SIZE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_viewpager_wallet, null);
            LinearLayout page= (LinearLayout) view.findViewById(R.id.ll_container);
            FrameLayout circle= (FrameLayout) view.findViewById(R.id.fl_container);
            LinearLayout llContainer= (LinearLayout) view.findViewById(R.id.container);
            TextView desc= (TextView) view.findViewById(R.id.desc);
            TextView title= (TextView) view.findViewById(R.id.title);
            TextView btn= (TextView) view.findViewById(R.id.btn_next);
            TextView skip= (TextView) view.findViewById(R.id.skip);

            for (int i = 0; i < 4; ++i) {
                //圆点
                ImageView point = new ImageView(WalletHintActivity.this);
                point.setImageResource(R.drawable.pointer_white_selector);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                if (i > 0) {
                    params.leftMargin = DisplayUtil.dip2px(16.f);
                    point.setEnabled(false);
                }
                point.setLayoutParams(params);
                llContainer.addView(point);
            }
            switch (position) {

                case 0:
                    page.setBackgroundResource(R.drawable.ct_rounded_rec_big_blue);
                    circle.setBackgroundResource(R.drawable.shape_circle_blue);
                    setIcon(position,llContainer,4);
                    desc.setText(getString(R.string.wallet_intro_desc_1));
                    title.setText(getString(R.string.wallet));
                    btn.setText(getString(R.string.operation_continue));
                    break;
                case 1:
                    page.setBackgroundResource(R.drawable.ct_rounded_rec_big_orange);
                    circle.setBackgroundResource(R.drawable.shape_circle_orange);
                    setIcon(position,llContainer,4);
                    desc.setText(getString(R.string.wallet_intro_desc_2));
                    title.setText(getString(R.string.wallet_intro_title_2));
                    btn.setText(getString(R.string.operation_continue));
                    break;

                case 2:
                    page.setBackgroundResource(R.drawable.ct_rounded_rec_big_mango);
                    circle.setBackgroundResource(R.drawable.shape_circle_mango);
                    setIcon(position,llContainer,4);
                    desc.setText(getString(R.string.wallet_intro_desc_3));
                    title.setText(getString(R.string.transaction));
                    btn.setText(getString(R.string.operation_continue));
                    break;

                case 3:
                    page.setBackgroundResource(R.drawable.ct_rounded_rec_purple);
                    circle.setBackgroundResource(R.drawable.shape_circle_purple);
                    setIcon(position,llContainer,4);
                    desc.setText(getString(R.string.wallet_intro_desc_4));
                    title.setText(getString(R.string.withdraw));
                    btn.setText(getString(R.string.operation_got_it));
                    break;

            }

            skip.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (position){
                        case 0:
                            mViewPager.setCurrentItem(1);
                            break;
                        case 1:
                            mViewPager.setCurrentItem(2);
                            break;
                        case 2:
                            mViewPager.setCurrentItem(3);
                            break;
                        case 3:
                            finish();
                            break;
                    }
                }
            });
            container.addView(view);
            return view;
        }
        private void setIcon(int position,LinearLayout ll,int max){
            for (int i=0;i<max;++i){
                if (i==position){
                    ll.getChildAt(i).setEnabled(true);
                }else{
                    ll.getChildAt(i).setEnabled(false);
                }
            }
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }


}
